

#ifndef TFS_FLASH_H__
#define TFS_FLASH_H__

#include <stdint.h>
#include <stdbool.h> 

#define TFS_FLASH_PAGE_SIZE				(256)
#define TFS_FLASH_PAGE_MASK				(TFS_FLASH_PAGE_SIZE - 1)

#define TFS_FLASH_SECTOR_SIZE			(4096)
#define TFS_FLASH_SECTOR_MASK			(~(TFS_FLASH_SECTOR_SIZE - 1))
#define TFS_FLASH_SECTOR_BEGIN_MASK     (TFS_FLASH_SECTOR_SIZE - 1)

static inline bool tfs_flash_check_is_same_sector(uint32_t addr1, uint32_t addr2)
{
	return (addr1 & TFS_FLASH_SECTOR_MASK ) == (addr2 & TFS_FLASH_SECTOR_MASK); 
}
static inline uint32_t tfs_flash_get_next_sector(uint32_t addr)
{
	return (addr + TFS_FLASH_SECTOR_SIZE) & TFS_FLASH_SECTOR_MASK;
}
static inline uint32_t tfs_flash_get_sector_with_cnt(uint32_t addr, uint32_t cnt)
{
	return (addr + TFS_FLASH_SECTOR_SIZE * cnt) & TFS_FLASH_SECTOR_MASK;
}
static inline bool tfs_flash_check_is_beginning_of_sector(uint32_t addr)
{
	return (addr & TFS_FLASH_SECTOR_BEGIN_MASK ) == 0;
}
static inline uint32_t tfs_flash_check_page_remain_bytes_to_write(uint32_t addr)
{
	return (TFS_FLASH_PAGE_SIZE - (addr & TFS_FLASH_PAGE_MASK) );
}
static inline uint32_t tfs_flash_sector_add_cnt(uint32_t addr, uint32_t num)
{
	return (addr + TFS_FLASH_SECTOR_SIZE * num );
}
static inline uint32_t tfs_flash_get_sector_cnt(uint32_t start, uint32_t end)
{
	return (end - start) / TFS_FLASH_SECTOR_SIZE; 
}
static inline uint32_t tfs_flash_set_addr_align_to_sector(uint32_t addr)
{
    return (addr & TFS_FLASH_SECTOR_MASK );
}

#endif 
